package ui;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import util.GuiUtilities;

public class TableRender extends DefaultTableCellRenderer {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Component getTableCellRendererComponent(JTable table,Object value,
			boolean isSelected,boolean hasFocus,int row,int col){
		try{
		Component comp=super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,col);
		Double d=(Double) table.getModel().getValueAt(row,7);
		
		String fechaEntregaString=table.getModel().getValueAt(row,4).toString();
		DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		Date fechaEntrega= sdf.parse(fechaEntregaString);
		Calendar fechaEntregaCal=Calendar.getInstance();
		fechaEntregaCal.setTime(fechaEntrega);
		
		Calendar calNow = Calendar.getInstance();
		
		int diasToAdd=d.intValue();
		calNow.add(Calendar.DAY_OF_MONTH, diasToAdd);
		
		if(calNow.after(fechaEntregaCal)){
			if(!table.getModel().getValueAt(row,3).toString().equals("Finalizado")){
			comp.setForeground(Color.red);
			}else{
				comp.setForeground(Color.black);
			}
		}else{
			comp.setForeground(Color.black);
		}
		return comp;
		}catch(Exception e){
			GuiUtilities.showError(this,e.getMessage());
			return null;
		}
	}
}
